package cn.iocoder.yudao.module.erp.controller.admin.gi.gidx;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gidx.vo.GiDxPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gidx.vo.GiDxRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gidx.vo.GiDxSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiDxDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.giitem.GiItemDxDO;
import cn.iocoder.yudao.module.erp.service.gi.GiDxService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - ERP东线口岸仓库")
@RestController
@RequestMapping("/erp/gi-dx")
@Validated
public class GiDxController {

    @Resource
    private GiDxService giDxService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP东线口岸仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:create')")
    public CommonResult<String> createGiDx(@Valid @RequestBody GiDxSaveReqVO createReqVO) {
        return success(giDxService.createGiDx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP东线口岸仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:update')")
    public CommonResult<Boolean> updateGiDx(@Valid @RequestBody GiDxSaveReqVO updateReqVO) {
        giDxService.updateGiDx(updateReqVO);
        return success(true);
    }

    @PutMapping("/examine")
    @Operation(summary = "ERP物流东线仓库单据审核")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:examine')")
    public CommonResult<Boolean> examineGiDx(@Valid @RequestBody GiDxDO giDxDO) {
        giDxService.examineGiDx(giDxDO);
        return success(true);
    }
    
    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP东线口岸仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:delete')")
    public CommonResult<Boolean> deleteGiDx(@RequestParam("id") String id) {
        giDxService.deleteGiDx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP东线口岸仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:query')")
    public CommonResult<GiDxRespVO> getGiDx(@RequestParam("id") String id) {
        GiDxDO giDx = giDxService.getGiDx(id);
        return success(BeanUtils.toBean(giDx, GiDxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP东线口岸仓库分页")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:query')")
    public CommonResult<PageResult<GiDxRespVO>> getGiDxPage(@Valid GiDxPageReqVO pageReqVO) {
        PageResult<GiDxDO> pageResult = giDxService.getGiDxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GiDxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP东线口岸仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiDxExcel(@Valid GiDxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GiDxDO> list = giDxService.getGiDxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP东线口岸仓库.xls", "数据", GiDxRespVO.class,
                        BeanUtils.toBean(list, GiDxRespVO.class));
    }

    // ==================== 子表（ERP物流东线仓库货号详情） ====================

    @GetMapping("/gi-item-dx/list-by-no")
    @Operation(summary = "获得ERP物流东线仓库货号详情列表")
    @Parameter(name = "no", description = "入库编号")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dx:detail')")
    public CommonResult<List<GiItemDxDO>> getGiItemDxListByNo(@RequestParam("no") String no) {
        return success(giDxService.getGiItemDxListByNo(no));
    }

}